<?php 
	class ShowControl extends CommonControl{
		public function index(){
			//分配top数据
			$this->assign_data();
			if(empty($_GET['asid']))$this->error('问题不存在');
			$asid=$_GET['asid'];
			$data=M('ask')->where('asid='.$asid)->find();
			if(!$data)$this->error('问题不存在');
			$user=M('user')->where('uid='.$data['uid'])->find();
			$data['username']=$user['username'];
			$data['time']=date('Y-m-d',$data['time']);
			$data['level']=$this->exp_to_level($user);
			
			
			
			// 统计回答数变量加入模板
			$this->assign('data1',$data);
			// 所有回答
			// $allanswer=M('answer')->where('asid='.$asid)->all();
			//回答者数据
		$allanswer=M('answer')->query("SELECT *  from hd_answer as a inner join hd_user as u on a.uid=u.uid where   asid={$asid}" );
		foreach ($allanswer as $k => $v) {
			$allanswer[$k]['face']=$this->face($v);
		}
		// 最佳回答者数据
		$best=M('answer')->query("SELECT *  from hd_answer as a join hd_user as u on a.uid=u.uid where a.accept=1 and asid={$asid}" );
		// var_dump($best);die;
		if($best){
		$best=$best[0];
		$best['ratio']=$this->ratio($best);
		$best['face']=$this->face($best);
		$best['level']=$this->exp_to_level($best);
		
		$this->assign('best',$best);
		}
		$this->assign('data',$data);
			$this->assign('answer',$allanswer);
		
		// var_dump($best);die;
		// 找到父级分类
		$cid=$data['cid'];
		// var_dump($data);die;
		$father_cate1=$this->get_cate($cid);
		// var_dump($father_cate1);die;
		$this->assign('father_cate',$father_cate1);
		//未解决问题
		$field='asid,content,answer,cid,time';
		$nosolve=M('ask')->where('solve=0 and cid='.$cid)->order('time DESC')->field($field)->limit(4)->all();
		foreach ($nosolve as $k =>$v) {
			$nosolve[$k]['time']=date('Y-m-d',$v['time']);
		}
		// var_dump($nosolve);die;
		$this->assign('nosolve',$nosolve);

			
			
			$this->display();
		}
		/**
		 * [answer 回答]
		 * @return [type] [description]
		 */
		public function answer(){
			if (!IS_POST)$this->error('问题不存在');
			$asid=$_POST['asid'];
			$content=trim(adds_html($_POST['content']));
			// $ask=M('ask')->where('asid='.$asid)->find();
			// $user=M('user')->where('uid='.$ask['uid'])->find();
			$uid=(int)$_SESSION['uid'];
			$data1=array(
				'content'=>$content,
				'uid'=>$uid,
				'time'=>time(),
				'asid'=>$asid

				);
			$bloon=M('answer')->add($data1);
			//修改用户信息（金币，经验，回答数）
			// $userDb = M('user');
			// $userDb->inc('point', "uid=$uid", C('GOLD_ANSWER'));
			// $userDb->inc('answer', "uid=$uid", 1);
			// $userDb->inc('exp', "uid=$uid", C('LV_ANSWER'));

			// M('ask')->inc('answer', "asid=$asid", 1);
			// $bloon=M('')
			$db=M('user');
			$gold=(int)C('GOLD_ANSWER');
			$lv=(int)C('LV_ANSWER');
			//修改回答用户加金币
			$d1=$db->exec("UPDATE hd_user SET point=point+{$gold} WHERE uid={$uid}");
			//修改回答用户加回答数
			$d2=$db->exec("UPDATE hd_user SET answer=answer+1 WHERE uid={$uid}");
			//修改回答用户加回答经验
			$d3=$db->exec("UPDATE hd_user SET exp=point+{$lv} WHERE uid={$uid}");
			//修改问题回答数
			$d4=$db->exec("UPDATE hd_ask SET answer=answer+1 WHERE asid={$asid}");
			// var_dump($d1,$d2,$d3,$d4);die;
			// 因为金币和经验可能为空，会被误认为false只判断3个
			if($d2&&$d4&&$bloon)$this->error('回答成功');


		}
		/**
		 * [accept 采纳]
		 * @return [type] [description]
		 */
		public function accept(){
			if(!empty($_GET)){
				$anid=(int)$_GET['anid'];
				$bloon=M('answer')->query("SELECT * FROM hd_answer as an join hd_ask as ask on an.asid=ask.asid where anid={$anid} and solve=0 and accept=0 ");
				$asid=(int)$bloon[0]['asid'];
				if($bloon!=null){
					// 修改问题解决状态
					$bloon1=M('answer')->where('anid='.$bloon[0]['anid'])->update(array('accept'=>1));
					// 修改答案采纳状态
					$bloon2=M('ask')->where('asid='.$bloon[0]['asid'])->update(array('solve'=>1));
					//修改提问用户信息
		// $askUid = M('ask')->where(array('asid'=>$asid))->getField('uid');
		// $userDb = M('user');

		// $userDb->inc('point', "uid=$askUid", C('GOLD_ACCEPT')); 
		// $userDb->inc('exp', "uid=$askUid", C('LV_ACCEPT'));

		// //修改回答用户信息
		// $reward = M('ask')->where(array('asid'=>$asid))->getField('reward');

		// $anUid = M('answer')->where(array('anid'=>$anid))->getField('uid');
		// $userDb->inc('point', "uid=$anUid", C('GOLD_ACCEPT'));
		// $userDb->inc('exp', "uid=$anUid", C('LV_ACCEPT'));
		// $userDb->inc('accept', "uid=$anUid", 1);
		// $userDb->inc('point', "uid=$anUid", $reward);
		// 获得提问者的uid
		// //查询出来的是数组
				$askuid=M('ask')->where('asid='.$asid)->field('uid')->find();
				$askuid=$askuid['uid'];
				
				// 实例化数据库模型函数
				$userdb=M('user');
				// 将c函数里面采纳问题金币经验转整
				$gold=(int)C('GOLD_ACCEPT');
				$exp=(int)C('LV_ACCEPT');
				// 执行修改金币函数
				$db1=$userdb->exec("UPDATE hd_user SET point=point+{$gold} WHERE uid={$askuid}");
				//修改经验
				$db2=$userdb->exec("UPDATE hd_user SET exp=exp+{$exp} WHERE uid={$askuid}");

				//修改回答者信息
				$reward=M('ask')->where('asid='.$asid)->field('reward')->find();
				$reward=(int)$reward['reward'];
				$anuid=M('answer')->where('anid='.$anid)->field('uid')->find();
				$anuid=$anuid['uid'];
				//修改金币
				$db3=$userdb->exec("UPDATE hd_user SET point=point+{$gold} WHERE uid={$anuid}");
				//修改经验
				$db4=$userdb->exec("UPDATE hd_user SET exp=exp+{$exp} WHERE uid={$anuid}");
				//修改用户被采纳数
				$db5=$userdb->exec("UPDATE hd_user SET accept=accept+1 WHERE uid={$anuid}");
				//获得被采纳的金币
				$db6=$userdb->exec("UPDATE hd_user SET point=point+{$reward} WHERE uid={$anuid}");
				
				



				if($bloon1&&$bloon2&&$db5)$this->error('采纳成功');
				}else{
					$this->error('非法操作');
				}

			}
			
		}
	}


 ?>